<?php
/**
 * 拼接图片路径的方法
 * Author: lyun
 * Date: 2018/11/19
 * Created by Panxsoft.
 */

namespace App\Services\Utils;

class ImageHelper
{
    const CACHE_CONNECTION_SYMBOL = '-';
    const CACHE_IMAGE_DIR = '/image/cache/';

    const CK_FINDER_OSS = 'oss';

    /**
     * 返回 OC 商城目录中的图片地址
     *
     * @param $route
     * @return string
     */
    public static function generateImagePath($route)
    {
        require_once(base_path('../dw-oc/admin/config.php'));

        if (defined('CK_FINDER_BACKEND') && CK_FINDER_BACKEND === self::CK_FINDER_OSS) {
            return self::getObjectByOss($route);
        } else {
            return self::toOpenCartUrl($route);
        }
    }

    /**
     * @deprecated (废弃，用上面的)
     *
     * @param $path
     * @return string
     */
    public static function generateCacheImagePath($path)
    {
        $path_info = pathinfo($path);

        // todo 需要返回指定尺寸的缓存图片？
        $width = request()->width ?? 300;
        $height = request()->height ?? 300;

        // http://dw.me/image/cache/catalog/demo/product/product-7-300x300.png
        $image_name = $path_info['dirname'] . '/' . $path_info['filename'] . self::CACHE_CONNECTION_SYMBOL . $width . 'x' . $height . '.' . $path_info['extension'];

        $cache_url_img = config('app.web-url') . self::CACHE_IMAGE_DIR . $image_name;

        if (!@fopen($cache_url_img, 'r')) // 没有显示原图？
        {
            $cache_url_img = self::generateImagePath($path);
        }

        return self::linkUrlDecode($cache_url_img);
    }

    /**
     * 中文url  dw.panxsoft.com/image/catalog/Glass/太阳镜01_01_9064-C660-80-300x300.jpg
     * 安卓能直接显示，ios需要转换
     * @link http://www.php.cn/php-weizijiaocheng-392641.html
     * @param $url
     * @return string dw.me/image/catalog/Glass/%E5%A4%AA%E9%98%B3%E9%95%9C01_01_9064-C660-80-300x300.jpg
     */
    public static function linkUrlDecode($url)
    {
        $uri = '';
        $cs = unpack('C*', $url);
        $len = count($cs);
        for ($i = 1; $i <= $len; $i++) {
            // todo  空格等于32字节 , 要是其他元素也等于32 ？
            $uri .= ($cs[$i] > 127 || $cs[$i] === 32) ? '%' . strtoupper(dechex($cs[$i])) : $url{$i - 1};
        }
        return $uri;
    }

    /**
     * @param $img_path
     *
     * @return string
     */
    public static function toOpenCartUrl($img_path): string
    {
        if (is_int(strpos($img_path,'http'))) {
            $image = self::linkUrlDecode($img_path);
        } else {
            $image = self::linkUrlDecode(config('app.web-url') . '/image/' . $img_path);
        }

        return $image;
    }

    /**
     * @param $img_path
     * @return null|string
     */
    public static function getObjectByOss($img_path)
    {
        if (is_int(strpos($img_path,'http'))) {
            $image = self::linkUrlDecode($img_path);
        } else {
            if (!defined('IMAGE_BASE_URL')) return null;
            $image = self::linkUrlDecode(IMAGE_BASE_URL.$img_path);
        }

        return $image;
    }

    /**
     *  去除头像的http头和参数
     *
     * @param $filename
     * @return mixed
     */
    public static function deleteHeaderAndParameter($filename)
    {
        if (defined('IMAGE_BASE_URL')){
            if(is_int(strpos($filename,'http'))) {
                $filename = explode(IMAGE_BASE_URL, $filename)[1];
            }
        }

        if(is_int(strpos($filename,'http'))) {
            $filename = explode(DIR_IMAGE, $filename)[1];
        }

        return explode('?', $filename)[0];
    }
}